{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/18_create_landsat_timelapse.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube(\"OwjSJnGWKJs\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Update the geemap package\n",
    "\n",
    "If you run into errors with this notebook, please uncomment the line below to update the [geemap](https://github.com/gee-community/geemap#installation) package to the latest version from GitHub. \n",
    "Restart the Kernel (Menu -> Kernel -> Restart) to take effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "## Create an interactive map\n",
    "\n",
    "### Use the Drawing tool to draw a rectangle on the map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "## Generate a Landsat timelapse animation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "if not os.path.exists(out_dir):\n",
    "    os.makedirs(out_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "label = \"Urban Growth in Las Vegas\"\n",
    "Map.add_landsat_ts_gif(\n",
    "    label=label,\n",
    "    start_year=1985,\n",
    "    bands=[\"Red\", \"Green\", \"Blue\"],\n",
    "    font_color=\"white\",\n",
    "    frames_per_second=10,\n",
    "    progress_bar_color=\"blue\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "## Create Landsat timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "You and define an roi or draw a rectangle on the map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = ee.Geometry.Polygon(\n",
    "    [\n",
    "        [\n",
    "            [-115.471773, 35.892718],\n",
    "            [-115.471773, 36.409454],\n",
    "            [-114.271283, 36.409454],\n",
    "            [-114.271283, 35.892718],\n",
    "            [-115.471773, 35.892718],\n",
    "        ]\n",
    "    ],\n",
    "    None,\n",
    "    False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# roi = Map.draw_last_feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = geemap.landsat_timeseries(\n",
    "    roi=roi, start_year=1985, end_year=2019, start_date=\"06-10\", end_date=\"09-20\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(collection.size().getInfo())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "first_image = collection.first()\n",
    "\n",
    "vis = {\"bands\": [\"NIR\", \"Red\", \"Green\"], \"min\": 0, \"max\": 4000, \"gamma\": [1, 1, 1]}\n",
    "\n",
    "Map.addLayer(first_image, vis, \"First image\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20",
   "metadata": {},
   "source": [
    "## Download ImageCollection as a GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define arguments for animation function parameters.\n",
    "video_args = {\n",
    "    \"dimensions\": 768,\n",
    "    \"region\": roi,\n",
    "    \"framesPerSecond\": 10,\n",
    "    \"bands\": [\"NIR\", \"Red\", \"Green\"],\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"gamma\": [1, 1, 1],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "work_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "if not os.path.exists(work_dir):\n",
    "    os.makedirs(work_dir)\n",
    "out_gif = os.path.join(work_dir, \"landsat_ts.gif\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.download_ee_video(collection, video_args, out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "## Add animated text to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "texted_gif = os.path.join(work_dir, \"landsat_ts_text.gif\")\n",
    "geemap.add_text_to_gif(\n",
    "    out_gif,\n",
    "    texted_gif,\n",
    "    xy=(\"3%\", \"5%\"),\n",
    "    text_sequence=1985,\n",
    "    font_size=30,\n",
    "    font_color=\"#ffffff\",\n",
    "    add_progress_bar=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "label = \"Urban Growth in Las Vegas\"\n",
    "geemap.add_text_to_gif(\n",
    "    texted_gif,\n",
    "    texted_gif,\n",
    "    xy=(\"2%\", \"88%\"),\n",
    "    text_sequence=label,\n",
    "    font_size=30,\n",
    "    font_color=\"#ffffff\",\n",
    "    progress_bar_color=\"cyan\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(texted_gif)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
